<?php

namespace App\Http\Controllers;

use App\Library\Tools\Upload;
use App\Traits\Msg;
use Illuminate\Http\Request;

class PublicController extends Controller
{

    use Msg;

    /**
     * 图片上传处理
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
//    public function uploadImg1(Request $request)
//    {
//        $file = $request->file('file');
//        $data = Upload::curlFile($file, 'img');
//        if ($data['code'] == 0) {
//            return $this->json($data, 0, $data['msg']);
//        }
//        return $this->json([], 1, $data['msg']);
//    }




    //图片上传处理
    public function uploadImg(Request $request)
    {

        $upload_path = config('services.upload_path');
        //上传文件最大大小,单位M
        $maxSize = 10;

        //支持的上传图片类型
        $allowed_extensions = ["png", "jpg", "gif"];
        //返回信息json
        $data = ['code'=>200, 'msg'=>'上传失败', 'data'=>''];
        $file = $request->file('file');

        //检查文件是否上传完成
        if ($file->isValid()){
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext),$allowed_extensions)){
                $data['msg'] = "请上传".implode(",",$allowed_extensions)."格式的图片";
                return response()->json($data);
            }
            //检测图片大小
            if ($file->getSize() > $maxSize*1024*1024){
                $data['msg'] = "图片大小限制".$maxSize."M";
                return response()->json($data);
            }
        }else{
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }

        $newFile = md5_file($file->getRealPath()).'.'.(strtolower($ext));

        //$newFile = date('Y-m-d')."_".time()."_".uniqid().".".$file->getClientOriginalExtension();


//        $disk = QiniuStorage::disk('qiniu');
//        $res = $disk->put($newFile,file_get_contents($file->getRealPath()));

        if(move_uploaded_file($file->getRealPath(), $upload_path.$newFile)){
            $data = [
                'code'  => 0,
                'msg'   => '上传成功',
                'file'  => $newFile,
                'path'  => $upload_path.$newFile,
                'url'   => $_SERVER['HTTP_HOST'].'/'.$upload_path.$newFile
            ];
        }else{
            $data['data'] = $file->getErrorMessage();
        }
        return $data;
    }





}
